"""
解题思路

对于这道题，我们稍微的将问题做一下转换，便可以抽象成数组长度为l，
选取连续的k-l的总和得分最少的连续子数组；问题自然可以求解成功。

"""



class Solution:
    def maxScore(self, cardPoints, k: int) -> int:
        window = len(cardPoints) - k

        new_scores = sum(cardPoints[:window])
        total_scores, min_scores = new_scores, new_scores

        for i in range(k):
            total_scores += cardPoints[i + window]
            new_scores += cardPoints[i + window] - cardPoints[i]
            min_scores = min(min_scores, new_scores)

        return total_scores - min_scores
